package com.arknights.web.controller;

import com.arknights.web.domain.User;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/llservlet")
public class llServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        ObjectMapper om = new ObjectMapper();
        User user = new User();
        user.setId("1");
        user.setName("xyj");
        String[] arr ={"mysql","oracle","redis"};
        User user1 = new User();
        user1.setId("2");
        user1.setName("zrq");
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        String s = om.writeValueAsString(arr);
        String s1 = om.writeValueAsString(user);
        String s2 = om.writeValueAsString(users);

        String num = request.getParameter("num");
        if(num.equals("0")){
            response.getWriter().print("0");
        }else if(num.equals("1")){
            response.getWriter().print(s);
        }else if (num.equals("2")){
            response.getWriter().print(s1);
        }else {
            response.getWriter().print(s2);
        }

//        }

    }
}
